home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / mx / messaging / config / ServerConfig.as < prev   
Encoding:
Text File  |  2008-05-21  |  9.7 KB  |  293 lines

  1. package mx.messaging.config
  2. {
  3.    import flash.utils.describeType;
  4.    import flash.utils.getDefinitionByName;
  5.    import mx.core.mx_internal;
  6.    import mx.messaging.Channel;
  7.    import mx.messaging.ChannelSet;
  8.    import mx.messaging.errors.InvalidChannelError;
  9.    import mx.messaging.errors.InvalidDestinationError;
  10.    import mx.messaging.errors.MessagingError;
  11.    import mx.messaging.messages.IMessage;
  12.    import mx.utils.Translator;
  13.    
  14.    use namespace mx_internal;
  15.    
  16.    public class ServerConfig
  17.    {
  18.       public static var serverConfigData:XML;
  19.       
  20.       public static const CLASS_ATTR:String = "type";
  21.       
  22.       public static const URI_ATTR:String = "uri";
  23.       
  24.       private static const TRANSLATOR:Translator = Translator.getDefaultInstanceFor(ServerConfig);
  25.       
  26.       private static var _channelSets:Object = {};
  27.       
  28.       private static var _clusteredChannels:Object = {};
  29.       
  30.       private static var _unclusteredChannels:Object = {};
  31.       
  32.       public function ServerConfig()
  33.       {
  34.          super();
  35.       }
  36.       
  37.       public static function getProperties(param1:String, param2:String = null) : XMLList
  38.       {
  39.          var destination:XMLList = null;
  40.          var destinationId:String = param1;
  41.          var serviceId:String = param2;
  42.          destination = null;
  43.          if(serviceId == null)
  44.          {
  45.             destination = xml..destination.(@id == destinationId);
  46.          }
  47.          else
  48.          {
  49.             destination = xml[serviceId].destination.(@id == destinationId);
  50.          }
  51.          if(destination.length() > 0)
  52.          {
  53.             return destination.properties;
  54.          }
  55.          throw new InvalidDestinationError(TRANSLATOR.textOf("unknownDestinationForService",serviceId,destinationId));
  56.       }
  57.       
  58.       private static function internalGetChannelSet(param1:XML) : ChannelSet
  59.       {
  60.          var _loc2_:Array = null;
  61.          var _loc3_:XMLList = null;
  62.          var _loc4_:int = 0;
  63.          var _loc5_:int = 0;
  64.          var _loc6_:Boolean = false;
  65.          var _loc7_:String = null;
  66.          var _loc8_:ChannelSet = null;
  67.          _loc2_ = [];
  68.          _loc3_ = param1.channels.channel;
  69.          _loc4_ = int(_loc3_.length());
  70.          _loc5_ = 0;
  71.          while(_loc5_ < _loc4_)
  72.          {
  73.             _loc2_.push(_loc3_[_loc5_].@ref.toString());
  74.             _loc5_++;
  75.          }
  76.          _loc6_ = param1.properties.network.cluster.length() > 0 ? true : false;
  77.          _loc7_ = _loc2_.join(",") + ":" + _loc6_;
  78.          if(_loc7_ in _channelSets)
  79.          {
  80.             return _channelSets[_loc7_];
  81.          }
  82.          _loc8_ = new ChannelSet(_loc2_,_loc6_);
  83.          _channelSets[_loc7_] = _loc8_;
  84.          return _loc8_;
  85.       }
  86.       
  87.       private static function createChannel(param1:String) : Channel
  88.       {
  89.          var channels:XMLList = null;
  90.          var channelConfig:XML = null;
  91.          var className:String = null;
  92.          var uri:String = null;
  93.          var channel:Channel = null;
  94.          var channelClass:Class = null;
  95.          var channelId:String = param1;
  96.          channels = xml.channels.channel.(@id == channelId);
  97.          if(channels.length() == 0)
  98.          {
  99.             throw new InvalidChannelError(TRANSLATOR.textOf("unknownChannelWithId",channelId));
  100.          }
  101.          channelConfig = channels[0];
  102.          className = channelConfig.attribute(CLASS_ATTR).toString();
  103.          uri = channelConfig.endpoint[0].attribute(URI_ATTR).toString();
  104.          channel = null;
  105.          try
  106.          {
  107.             channelClass = getDefinitionByName(className) as Class;
  108.             channel = new channelClass(channelId,uri);
  109.             channel.applySettings(channelConfig);
  110.          }
  111.          catch(e:ReferenceError)
  112.          {
  113.             throw new InvalidChannelError(TRANSLATOR.textOf("unknownChannelClass",className));
  114.          }
  115.          return channel;
  116.       }
  117.       
  118.       public static function getChannel(param1:String, param2:Boolean = false) : Channel
  119.       {
  120.          var _loc3_:Channel = null;
  121.          if(!param2)
  122.          {
  123.             if(param1 in _unclusteredChannels)
  124.             {
  125.                return _unclusteredChannels[param1];
  126.             }
  127.             _loc3_ = createChannel(param1);
  128.             _unclusteredChannels[param1] = _loc3_;
  129.             return _loc3_;
  130.          }
  131.          if(param1 in _clusteredChannels)
  132.          {
  133.             return _clusteredChannels[param1];
  134.          }
  135.          _loc3_ = createChannel(param1);
  136.          _clusteredChannels[param1] = _loc3_;
  137.          return _loc3_;
  138.       }
  139.       
  140.       public static function set xml(param1:XML) : void
  141.       {
  142.          serverConfigData = param1;
  143.          _channelSets = {};
  144.          _clusteredChannels = {};
  145.          _unclusteredChannels = {};
  146.       }
  147.       
  148.       mx_internal static function getChannelSetClusterRequestParams(param1:ChannelSet) : Object
  149.       {
  150.          var _loc2_:String = null;
  151.          var _loc3_:XMLList = null;
  152.          var _loc4_:int = 0;
  153.          var _loc5_:int = 0;
  154.          var _loc6_:XML = null;
  155.          var _loc7_:* = null;
  156.          var _loc8_:XMLList = null;
  157.          var _loc9_:int = 0;
  158.          var _loc10_:int = 0;
  159.          var _loc11_:Object = null;
  160.          var _loc12_:String = null;
  161.          var _loc13_:Array = null;
  162.          _loc2_ = param1.channelIds.join(",");
  163.          if(_loc2_.length > 0)
  164.          {
  165.             _loc3_ = xml..destination;
  166.             _loc4_ = int(_loc3_.length());
  167.             _loc5_ = 0;
  168.             while(_loc5_ < _loc4_)
  169.             {
  170.                _loc6_ = _loc3_[_loc5_];
  171.                if(_loc6_.properties.network.cluster.length() > 0)
  172.                {
  173.                   _loc7_ = "";
  174.                   _loc8_ = _loc6_.channels.channel.@ref;
  175.                   _loc9_ = int(_loc8_.length());
  176.                   _loc10_ = 0;
  177.                   while(_loc10_ < _loc9_)
  178.                   {
  179.                      if(_loc10_ > 0)
  180.                      {
  181.                         _loc7_ += ",";
  182.                      }
  183.                      _loc7_ += _loc8_[_loc10_].toString();
  184.                      _loc10_++;
  185.                   }
  186.                   if(_loc7_.toString() == _loc2_)
  187.                   {
  188.                      _loc11_ = {};
  189.                      _loc11_.destination = _loc6_.@id.toString();
  190.                      _loc12_ = _loc6_.parent().@messageTypes.toString();
  191.                      _loc13_ = _loc12_.split(",");
  192.                      _loc11_.messageRefType = _loc13_[0];
  193.                      return _loc11_;
  194.                   }
  195.                }
  196.                _loc5_++;
  197.             }
  198.          }
  199.          return null;
  200.       }
  201.       
  202.       public static function get xml() : XML
  203.       {
  204.          return serverConfigData != null ? serverConfigData : <services/>;
  205.       }
  206.       
  207.       public static function getChannelSet(param1:String, param2:IMessage, param3:String = null) : ChannelSet
  208.       {
  209.          var destinations:XMLList = null;
  210.          var destinationCount:int = 0;
  211.          var alias:String = null;
  212.          var i:int = 0;
  213.          var serviceConfig:XML = null;
  214.          var types:String = null;
  215.          var aliases:Array = null;
  216.          var n:int = 0;
  217.          var j:int = 0;
  218.          var destinationId:String = param1;
  219.          var message:IMessage = param2;
  220.          var serverMessageType:String = param3;
  221.          destinations = xml..destination.(@id == destinationId);
  222.          destinationCount = int(destinations.length());
  223.          if(destinationCount == 0)
  224.          {
  225.             throw new InvalidDestinationError(TRANSLATOR.textOf("unknownDestination",destinationId));
  226.          }
  227.          if(destinationCount == 1)
  228.          {
  229.             return internalGetChannelSet(destinations[0]);
  230.          }
  231.          alias = serverMessageType != null ? serverMessageType : describeType(message).@alias.toString();
  232.          i = 0;
  233.          while(i < destinationCount)
  234.          {
  235.             serviceConfig = destinations[i].parent();
  236.             types = serviceConfig.@messageTypes.toString();
  237.             aliases = types.split(",");
  238.             n = int(aliases.length);
  239.             j = 0;
  240.             while(j < n)
  241.             {
  242.                if(aliases[j] == alias)
  243.                {
  244.                   return internalGetChannelSet(destinations[i]);
  245.                }
  246.                j++;
  247.             }
  248.             i++;
  249.          }
  250.          throw new InvalidDestinationError(TRANSLATOR.textOf("destinationWithInvalidMessageType",destinationId,alias));
  251.       }
  252.       
  253.       public static function getServiceIdForMessage(param1:IMessage) : String
  254.       {
  255.          var _loc2_:String = null;
  256.          var _loc3_:XMLList = null;
  257.          var _loc4_:int = 0;
  258.          var _loc5_:int = 0;
  259.          var _loc6_:XML = null;
  260.          var _loc7_:String = null;
  261.          var _loc8_:Array = null;
  262.          var _loc9_:int = 0;
  263.          var _loc10_:int = 0;
  264.          _loc2_ = describeType(param1).@alias.toString();
  265.          _loc3_ = xml.children();
  266.          _loc4_ = int(_loc3_.length());
  267.          _loc5_ = 0;
  268.          while(_loc5_ < _loc4_)
  269.          {
  270.             _loc6_ = _loc3_[_loc5_];
  271.             if(_loc6_.@messageTypes.length() != 0)
  272.             {
  273.                _loc7_ = _loc6_.@messageTypes.toString();
  274.                _loc8_ = _loc7_.split(",");
  275.                _loc9_ = int(_loc8_.length);
  276.                _loc10_ = 0;
  277.                while(_loc10_ < _loc9_)
  278.                {
  279.                   if(_loc8_[_loc10_] == _loc2_)
  280.                   {
  281.                      return _loc6_.localName();
  282.                   }
  283.                   _loc10_++;
  284.                }
  285.             }
  286.             _loc5_++;
  287.          }
  288.          throw new MessagingError(TRANSLATOR.textOf("noServiceForMessageType",_loc2_));
  289.       }
  290.    }
  291. }
  292.  
  293.